/* Title:		
 *
 * Project:     current
 *
 * @ Link: 		http://...
 *
 * @ Email:		ivan_ling@hotmail.com
 *
 * @ Copyright: Copyright (c) 2008 mezimedia
 *
 * @ Author 	Ivan.ling
 *
 * @ Version 	1.0
 
 * @ last change time 2008-01-20 	
 */
package com.pub.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import au.com.bytecode.opencsv.CSVReader;


public class FileUtils extends FilterUtils
{
	private static MessageDigest messagedigest;
	private static char md5Chars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	
	private WritableByteChannel fiOutChannel;
	private ByteBuffer bbBuffer;
	
	private String strFilePath;
	private int iBufferSize;

	public FileUtils()
	{
	}

	public FileUtils(String filePath)
    {
        this.strFilePath = filePath;
        
    }
 	
	public void initNioWriter(String fileName, int iBufferLength) throws FileNotFoundException
	{
		this.strFilePath 	= fileName;
		this.iBufferSize 	= iBufferLength;
		
		File fiFile = new File(this.strFilePath); 
		
	    FileOutputStream outputFile = new FileOutputStream(fiFile);
	    
	    this.fiOutChannel 	= Channels.newChannel(outputFile);
	    this.bbBuffer 		= ByteBuffer.allocate(this.iBufferSize);
	}

	public void print(String content) throws IOException
	{
		this.bbBuffer.put(content.getBytes());
		
		this.bbBuffer.flip();
		this.fiOutChannel.write(this.bbBuffer);
		this.bbBuffer.clear();
	}
	
	public void println(String content) throws IOException
	{
		this.bbBuffer.put(content.getBytes());
		this.bbBuffer.put(BT_ENTER);
		
		this.bbBuffer.flip();
		this.fiOutChannel.write(this.bbBuffer);
		this.bbBuffer.clear();
	}
	
	public void close() throws IOException
    {
		this.fiOutChannel.close();
    }

	public static Object readDat(String datFilePath) throws IOException, ClassNotFoundException
	{
		FileInputStream fiShare 	= null;
		ObjectInputStream oisShare 	= null;
		Object obReturn				= null;
		
		fiShare 	= new FileInputStream(datFilePath);
    	oisShare 	= new ObjectInputStream(fiShare);
    	obReturn 	= oisShare.readObject();
	
		if(oisShare != null)
			oisShare.close();
		if(fiShare != null)
			fiShare.close();
   		
   		return obReturn;
	}
	
	public static void writeDat(String datFilePath, Object obTarget) throws IOException
	{
		FileOutputStream fosShare 	= null;
		ObjectOutputStream oosShare = null;

	  	fosShare 	= new FileOutputStream(datFilePath);
	  	oosShare 	= new ObjectOutputStream(fosShare);

	  	oosShare.writeObject(obTarget);
		
		if(fosShare != null)
			fosShare.close();
	
		if(oosShare != null)
			oosShare.close();
	}
	
	public static Map<String, ArrayList<String>> getKeyValue(String pathAndFileName, char separator, char quotechar, int SkipLines, int iLenLimit, int iKcolumn, int iVcolumn) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashMap<String, ArrayList<String>> hmReturn = new HashMap<String, ArrayList<String>>(82);
		ArrayList<String> alCache;
		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= iLenLimit && !arrLineData[iKcolumn].trim().equals("") && !arrLineData[iVcolumn].trim().equals("")){
				if(hmReturn.containsKey(arrLineData[iKcolumn].trim())){
					alCache = hmReturn.get(arrLineData[iKcolumn].trim());
					alCache.add(arrLineData[iVcolumn].trim());
				}else{
					alCache = new ArrayList<String>();
					alCache.add(arrLineData[iVcolumn].trim());
					
					hmReturn.put(arrLineData[iKcolumn].trim(), alCache);
				}
			}
		}
		
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
		
		return hmReturn;
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<ArrayList<String>> getAll(String pathAndFileName, char separator, char quotechar, int SkipLines, int iLenLimit)
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		int i;
		
		ArrayList<ArrayList<String>> alReturn  = new ArrayList<ArrayList<String>>(62);
		ArrayList<String> alStrTpl = new ArrayList<String>(iLenLimit);
		ArrayList<String> alCache;
		
  		try {
  			frReader  = new FileReader(pathAndFileName);
  			csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
  			
  			while ((arrLineData = csvReader.readNext()) != null) {
				if(arrLineData.length >= iLenLimit){
					alCache = (ArrayList<String>)alStrTpl.clone();
					
					for(i = 0; i < arrLineData.length; i++){
						alCache.add(arrLineData[i].trim());
					}
	  				
	  				alReturn.add(alCache);
				}
			}
  		
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(csvReader != null)
					csvReader.close();
				if(frReader != null)
					frReader.close();
			} catch (IOException e) {
			}
		}
  		
		return alReturn;
	}
	

	@SuppressWarnings("unchecked")
	public static ArrayList<ArrayList<String>> getAll(String pathAndFileName, int iColumnLenLimit) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		int i;
		
		ArrayList<ArrayList<String>> alReturn  = new ArrayList<ArrayList<String>>(62);
		ArrayList<String> alStrTpl = new ArrayList<String>(iColumnLenLimit);
		ArrayList<String> alCache;
		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, CHAR_COMMA, CHAR_DOUBLE_QUOTATION, 0);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= iColumnLenLimit){
				alCache = (ArrayList<String>)alStrTpl.clone();
				
				for(i = 0; i < arrLineData.length; i++){
					alCache.add(arrLineData[i].trim());
				}
  				
  				alReturn.add(alCache);
			}
		}
	
		try {
			if(csvReader != null)
				csvReader.close();
			if(frReader != null)
				frReader.close();
		} catch (IOException e) {
		}
		
		return alReturn;
	}
	

	
	public static Map<String, HashSet<String>> getKeyValueSet(String pathAndFileName, char separator, char quotechar, int SkipLines, int iLenLimit, int iKcolumn, int iVcolumn) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashMap<String, HashSet<String>> hmReturn = new HashMap<String, HashSet<String>>(82);
		HashSet<String> hsCache;
		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= iLenLimit && !arrLineData[iKcolumn].trim().equals("") && !arrLineData[iVcolumn].trim().equals("")){
				if(hmReturn.containsKey(arrLineData[iKcolumn].trim())){
					hsCache = hmReturn.get(arrLineData[iKcolumn].trim());
					hsCache.add(arrLineData[iVcolumn].trim());
				}else{
					hsCache = new HashSet<String>();
					hsCache.add(arrLineData[iVcolumn].trim());
					
					hmReturn.put(arrLineData[iKcolumn].trim(), hsCache);
				}
			}
		}
		
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
		
		return hmReturn;
	}
	
	public static Set<String> getOneColumn(String pathAndFileName, char separator, char quotechar, int SkipLines, int iLenLimit) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashSet<String> hsReturn = new HashSet<String>(82);
 		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= iLenLimit && !arrLineData[0].trim().equals("")){
				hsReturn.add(arrLineData[0].trim());
			}
		}
	
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
			
		return hsReturn;
	}
	
	public static Set<String> getOneColumnToLower(String pathAndFileName, char separator, char quotechar, int SkipLines, int iLenLimit) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashSet<String> hsReturn = new HashSet<String>(82);
 		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= iLenLimit && !arrLineData[0].trim().equals("")){
				hsReturn.add(arrLineData[0].trim().toLowerCase());
			}
		}
	
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
			
		return hsReturn;
	}
	
	public static Set<String> getOneColumn(String pathAndFileName, int iColumnIndex) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashSet<String> hsReturn = new HashSet<String>(82);
 		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, CHAR_COMMA, CHAR_DOUBLE_QUOTATION, 0);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length > iColumnIndex && !arrLineData[iColumnIndex].trim().equals("")){
				hsReturn.add(arrLineData[iColumnIndex]);
			}
		}
	
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
			
		return hsReturn;
	}
	
	public static Map<String, Integer> getOneColumnCounter(String pathAndFileName, int iColumnIndex) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		int iC = 0;
		HashMap<String, Integer> hmReturn = new HashMap<String, Integer>(82);
 		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, CHAR_COMMA, CHAR_DOUBLE_QUOTATION, 0);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length > iColumnIndex && !arrLineData[iColumnIndex].trim().equals("")){
				iC++;
				
				hmReturn.put(arrLineData[iColumnIndex], iC);
			}
		}
	
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
			
		return hmReturn;
	}
	
	public static Map<String, String> getKeyValue(String pathAndFileName, char separator, char quotechar, int SkipLines, int iKeyColumn, int iValueColumn) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashMap<String, String> hmReturn = new HashMap<String, String>(82);
 		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= 2 && !arrLineData[iKeyColumn].trim().equals("")){
				hmReturn.put(arrLineData[iKeyColumn].trim(), arrLineData[iValueColumn].trim());
			}
		}
	
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
			
		return hmReturn;
	}

	public static Map<String, String> getKeyValueToLower(String pathAndFileName, char separator, char quotechar, int SkipLines, int iKeyColumn, int iValueColumn) throws IOException
	{
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		
		HashMap<String, String> hmReturn = new HashMap<String, String>(82);
 		
		frReader  = new FileReader(pathAndFileName);
		csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
		
		while ((arrLineData = csvReader.readNext()) != null) {
			if(arrLineData.length >= 2 && !arrLineData[iKeyColumn].trim().equals("")){
				hmReturn.put(arrLineData[iKeyColumn].trim().toLowerCase(), arrLineData[iValueColumn].trim().toLowerCase());
			}
		}
	
		if(csvReader != null)
			csvReader.close();
		if(frReader != null)
			frReader.close();
			
		return hmReturn;
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<Set<String>> getKeyToLower (String pathAndFileName, char separator, char quotechar, int SkipLines, int iLenLimit)
	{
		ArrayList<Set<String>> alReturn = null;
		
		CSVReader csvReader = null;
		FileReader frReader = null;
		String[] arrLineData;
		String strCache;
		int i;
		
		alReturn  = new ArrayList<Set<String>>(16);
		HashSet<String> hsStrTpl = new HashSet<String>(62);
		Set<String> hsCache;
		
  		try {
  			frReader  = new FileReader(pathAndFileName);
  			csvReader = new CSVReader(frReader, separator, quotechar, SkipLines);
  			
  			while ((arrLineData = csvReader.readNext()) != null) {
				if(arrLineData.length >= iLenLimit){
	  				for(i = 0; i < arrLineData.length; i++){
						if(!arrLineData[i].trim().equals("")){
							if(i <= (alReturn.size() - 1)){
								hsCache = alReturn.get(i);
								strCache = WordUtils.strDelAdditionalSpace(arrLineData[i].trim().toLowerCase());
								if(strCache == null || strCache.equals(""))
									continue;
								else
									hsCache.add(strCache);
							}else{
								hsCache = (HashSet<String>)hsStrTpl.clone();
								strCache = WordUtils.strDelAdditionalSpace(arrLineData[i].trim().toLowerCase());
								if(strCache == null || strCache.equals(""))
									continue;
								else
									hsCache.add(strCache);
								alReturn.add(hsCache);
							}
						}
					}
				}
			}
  		
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(csvReader != null)
					csvReader.close();
				if(frReader != null)
					frReader.close();
			} catch (IOException e) {
			}
		}
  		
		return alReturn;
	}
	
	/*
     * Moving a File to Another Directory
     * @param srcFile  eg: c:\windows\abc.txt
     * @param destPath eg: c:\temp
     * @return success 
     */
    public static boolean move(String srcFile, String destPath){
        File fiSource = new File(srcFile);      //-- File (or directory) to be moved
        File fiDir = new File(destPath);        //-- Destination directory
        
        return fiSource.renameTo(new File(fiDir, fiSource.getName()));//-- Move file to new directory
    }

	/*
     * Moving a File to Another Directory
     * @param srcFile  eg: c:\windows\abc.txt
     * @param destPath eg: c:\temp
     * @param strDestName destination file name
     * @return success 
     */
    public static boolean move(String srcFile, String destPath, String strDestName){
        File fiSource = new File(srcFile);      //-- File (or directory) to be moved
        File fiDir = new File(destPath);        //-- Destination directory
        
        return fiSource.renameTo(new File(fiDir, strDestName));//-- Move file to new directory
    }
    
  	/*
     * Moving a File to Another Directory
     * @param destPath eg: 	c:\temp
     * @param iReturnSize 	return data size
     * @return destPath all files 
     */
    public static ArrayList<String> SearchFilesFromDIR(String destPath, int iReturnSize){
    	Stack<File> stCache = new Stack<File>();
    	ArrayList<String> alReturn = new ArrayList<String>();
    	if(iReturnSize < 1)
    		alReturn = new ArrayList<String>();
    	else
    		alReturn = new ArrayList<String>(iReturnSize);
    	String[] arrFileNameCache;
    	
    	int i;
    	File fiDir= new File(destPath);
    	if(fiDir.isDirectory()){
    		stCache.push(fiDir);
    		while(!stCache.isEmpty()){
    			fiDir = stCache.pop();
    			
				arrFileNameCache = fiDir.list();
				for(i = 0; i < arrFileNameCache.length; i++){
					fiDir= new File(arrFileNameCache[i]);
					if(fiDir.isDirectory()){
	    				stCache.push(fiDir);
	    			}else
	    				alReturn.add(arrFileNameCache[i]);
				}
    		}
    		return alReturn;
    	}else
    		return null;
    }    
    
	/*
     * Moving a File to Another Directory
     * @param destPath 		dest path :eq c:\temp
     * @param iReturnSize 	return data size
     * @param strFilterType filter file type
     * @param isIgnoreCase 	ignore file type case
     * @return all files in dir destpath
     */
    public static ArrayList<String> SearchFilesFromDIR(String destPath, int iReturnSize, String strFilterType, boolean isIgnoreCase){
    	Stack<File> stCache = new Stack<File>();
    	ArrayList<String> alReturn = new ArrayList<String>();
    	if(iReturnSize < 1)
    		alReturn = new ArrayList<String>();
    	else
    		alReturn = new ArrayList<String>(iReturnSize);
    	String[] arrFileNameCache;
    	
    	int i;
    	File fiDir= new File(destPath);
    	if(fiDir.isDirectory()){
    		stCache.push(fiDir);
    		while(!stCache.isEmpty()){
    			fiDir = stCache.pop();
    			
				arrFileNameCache = fiDir.list();
				for(i = 0; i < arrFileNameCache.length; i++){
					fiDir= new File(arrFileNameCache[i]);
					if(fiDir.isDirectory()){
	    				stCache.push(fiDir);
	    			}else{
	    				if(isIgnoreCase){
	    					if(arrFileNameCache[i].toLowerCase().endsWith(strFilterType.toLowerCase()))
	    						alReturn.add(arrFileNameCache[i]);
	    				}else{
	    					if(arrFileNameCache[i].endsWith(strFilterType))
	    						alReturn.add(arrFileNameCache[i]);
	    				}
	    			}
				}
    		}
    		return alReturn;
    	}else
    		return null;
    }
    
    /** new dir 
     * 
     * @param folderPath String sample c:/fqf 
     * @return boolean 
     */ 
   public static void createFolder(String folderPath) 
   { 
	   File fiPath = new File(folderPath); 
       if (!fiPath.exists()) {
    	   fiPath.mkdirs();
       } 
   } 

   public static String getFullPathById(String rootPath, int id, String fileName)
   {
	   String fullPath = rootPath + getDirById(id);
	   
	   createFolder(fullPath);
	   
	   return fullPath + File.separatorChar + fileName;
   }
   
   public static String getDirById(int id)
   {
	   String idStr = Integer.toString(id);
	   
	   int len = idStr.length();
	   
	   String pos1_2, pos3_4;
	   
	   switch (len){
	   		case 1: pos1_2 = "0" + id; pos3_4 = "00"; break;
	   		case 2: pos1_2 = idStr; pos3_4 = "00"; break;
	   		case 3: pos1_2 = idStr.substring(1, 3); pos3_4 = "0" + idStr.substring(0, 1); break;
	   		default:pos1_2 = idStr.substring(len - 2, len); pos3_4 = idStr.substring(len - 4, len - 2); break;
	   }
	   
 	   return Integer.toString(id % 10) + File.separatorChar + pos3_4 + File.separatorChar + pos1_2;
   }
   
   	public static String getFileMD5String(File file) throws Exception
   	{
   		messagedigest 		= MessageDigest.getInstance("MD5");
   		FileInputStream in 	= new FileInputStream(file);
	   
   		FileChannel ch 		= in.getChannel();
   		MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
	   
   		messagedigest.update(byteBuffer);
	   
   		return bufferToHex(messagedigest.digest());
   	}
   
   	public static String getFileMD5(File file) 
   	{
   		if (!file.isFile()){
   			return null;
   		}
   		
   	    MessageDigest digest = null;
   	    FileInputStream in=null;
   	    byte buffer[] = new byte[1024];
   	    int len;
   	    
   	    try {
   	    	digest = MessageDigest.getInstance("MD5");
   	    	in = new FileInputStream(file);
   	    	while ((len = in.read(buffer, 0, 1024)) != -1) {
   	    		digest.update(buffer, 0, len);
   	    	}
   	    	
   	    	in.close();
   	    } catch (Exception e) {
   	    	e.printStackTrace();
   	    	return null;
   	    }
   	    
   	    BigInteger bigInt = new BigInteger(1, digest.digest());
   	    return bigInt.toString(16);
   	  }
   	  
   	  
	public static Map<String, String> getDirMD5(File file,boolean listChild) 
	{
		if(!file.isDirectory()){
			return null;
		}
   		  //<filepath,md5>
		Map<String, String> map=new HashMap<String, String>();
		String md5;
   		  
		File files[]=file.listFiles();
   		for(int i=0;i<files.length;i++){
   			  File f=files[i];
   			  if(f.isDirectory()&&listChild){
   				  map.putAll(getDirMD5(f, listChild));
   			  } else {
   				  md5=getFileMD5(f);
   				  if(md5!=null){
   					  map.put(f.getPath(), md5);
   				  }
   			  }
   		  }
   		  
   		  return map;
   	  }
   	  
   	public static boolean check(File f,String md5) throws Exception
   	{
	   if(getFileMD5String(f).equals(md5))
		   return true;
	   else
		   return false;
   	}
   
	private static String bufferToHex(byte bytes[])
	{
		return bufferToHex(bytes, 0, bytes.length);
	}
	
	private static String bufferToHex(byte bytes[], int m, int n)
	{
		StringBuffer stringbuffer = new StringBuffer(2 * n);
		int k = m + n;
		for (int l = m; l < k; l++)
		{
			appendHexPair(bytes[l], stringbuffer);
		}
		return stringbuffer.toString();
	}
	
	private static void appendHexPair(byte bt, StringBuffer stringbuffer)
	{
		char c0 = md5Chars[(bt & 0xf0) >> 4];
		char c1 = md5Chars[bt & 0xf];
		stringbuffer.append(c0);
		stringbuffer.append(c1);
	}
   
	public static String getStringMD5String(String str) throws Exception
	{
		messagedigest = MessageDigest.getInstance("MD5");
		messagedigest.update(str.getBytes()); 
		return bufferToHex(messagedigest.digest());
	}
	
   /** new file
     * 
     * @param filePathAndName String c:/fqf.txt 
     * @param fileContent String 
     * @return boolean 
     * @throws IOException 
     */ 
   public void newFile(String filePathAndName, String fileContent) throws IOException 
   { 
       File fiPath = new File(filePathAndName); 
       if (!fiPath.exists()) { 
    	   fiPath.createNewFile(); 
       }
       
       FileWriter resultFile 	= new FileWriter(fiPath); 
       PrintWriter myFile 		= new PrintWriter(resultFile); 
       
       myFile.println(fileContent); 
       resultFile.close(); 
  } 

   /** delete folder
     * 
     * @param filePathAndName String c:/fqf 
     * @param fileContent String 
     * @return boolean 
     */ 
   public static boolean delFolder(String folderPath) 
   { 
	   if(folderPath == null || folderPath.equals(""))
		   return false;
	   	
	   	delAllFile(folderPath);
		File myFilePath = new File(folderPath);
		if(myFilePath.exists())
			myFilePath.delete();
		else
			return false;
		
		return true;
   } 

   /** delete all files in folder
     * 
     * @param path String c:/fqf 
     */ 
   public static boolean delAllFile(String path) 
   { 
       File file = new File(path); 
       if (!file.exists()) { 
           return false; 
       } 
       if (!file.isDirectory()) { 
           return false; 
       } 
       String[] tempList = file.list(); 
       File temp = null; 
       for (int i = 0; i < tempList.length; i++) { 
           if (path.endsWith(File.separator)) { 
               temp = new File(path + tempList[i]); 
           } 
           else { 
               temp = new File(path + File.separator + tempList[i]); 
           } 
           if (temp.isFile()) { 
               temp.delete(); 
           } 
           if (temp.isDirectory()) { 
               delAllFile(path+"/"+ tempList[i]);
               delFolder(path+"/"+ tempList[i]);
           } 
       }
       
       return true;
   } 

   /** 
     *
     * @param oldPath String c:/fqf.txt 
     * @param newPath String f:/fqf.txt 
     * @return boolean 
     * @throws IOException 
     */ 
   public static boolean copyFile(String oldPath, String newPath) throws IOException 
   { 
	   	int bytesum  = 0; 
	   	int byteread = 0; 
	   	File oldfile = new File(oldPath);
	   	if (oldfile.exists()) {
	   		InputStream inStream;
	   		FileOutputStream fs; 
	   		
	   		File newfile = new File(newPath);
	   		if(newfile.isDirectory()){
	   			fs 	= new FileOutputStream(newPath + oldfile.getName());
	   		}else
	   			fs 	= new FileOutputStream(newfile);
	   		
	   		inStream 		= new FileInputStream(oldPath);
	   		byte[] buffer 	= new byte[1444]; 
	    	
	    	while ((byteread = inStream.read(buffer)) != -1) { 
	    		bytesum += byteread;
	    		
	    		fs.write(buffer, 0, byteread); 
	    	}
	    	inStream.close();
	    	
	    	return true;
   		}else
   			return false;
	}
   
   	/** 
	 *
	 * @param oldPath String c:/fqf.txt 
	 * @param newPath String f:/fqf.txt 
	 * @return boolean 
	 * @throws IOException 
	 */ 
	 public static boolean copyFile(File srcFile, String newPath, String fileName) throws IOException 
	 { 
	   	int bytesum  = 0; 
	   	int byteread = 0; 
	   	if (srcFile.exists()) {
	   		InputStream inStream;
	   		FileOutputStream fs; 
	   		
	   		File newfile = new File(newPath);
	   		if(!newfile.exists())
	   			newfile.mkdirs();
	   		
   	   		if(fileName == null){
	   			fs 	= new FileOutputStream(newPath + srcFile.getName());
	   		}else{
	   			fs 	= new FileOutputStream(newPath + fileName);
	   		}
	   		
	   		inStream 		= new FileInputStream(srcFile);
	   		byte[] buffer 	= new byte[1444]; 
	    	
	    	while ((byteread = inStream.read(buffer)) != -1) { 
	    		bytesum += byteread;
	    		
	    		fs.write(buffer, 0, byteread); 
	    	}
	    	inStream.close();
	    	
	    	return true;
 		}else
 			return false;
	} 

    /** delete file
    * 
    * @param filePathAndName String c:/fqf.txt 
    * @param fileContent String 
    * @return boolean 
    */ 

   public static boolean delFile(String filePathAndName)
   { 
	   	boolean isReturn = false;
	   	
	   	File fiDelFile = new File(filePathAndName);
	   	if(fiDelFile.exists()){
	   		if(fiDelFile.delete())
	   			isReturn = true;
	   	}
	   	
	   	return isReturn;
	}

   /** 
     *
     * @param oldPath String c:/fqf 
     * @param newPath String f:/fqf/ff 
     * @return boolean 
     * @throws IOException 
     */ 
   public static void copyFolder(String oldPath, String newPath) throws IOException
   { 
       (new File(newPath)).mkdirs();
       File a=new File(oldPath); 
       String[] file=a.list(); 
       File temp=null; 
       for (int i = 0; i < file.length; i++) { 
           if(oldPath.endsWith(File.separator)){ 
               temp=new File(oldPath+file[i]); 
           } 
           else{ 
               temp=new File(oldPath+File.separator+file[i]); 
           } 

           if(temp.isFile()){
               FileInputStream input = new FileInputStream(temp); 
               FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); 
               byte[] b = new byte[1024 * 5]; 
               int len; 
               while ( (len = input.read(b)) != -1) { 
                   output.write(b, 0, len); 
               } 
               output.flush(); 
               output.close(); 
               input.close(); 
           }
           
           if(temp.isDirectory()){
               copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 
           } 
       } 
   } 

   /** 
     *
     * @param oldPath String c:/fqf.txt 
     * @param newPath String d:/fqf.txt 
     * @throws IOException 
     */ 
   public static void moveFile(String oldPath, String newPath) throws IOException 
   { 
       copyFile(oldPath, newPath); 
       delFile(oldPath); 

   } 

   /** 
     *
     * @param oldPath String c:/fqf.txt 
     * @param newPath String d:/fqf.txt 
     * @throws IOException 
     */ 
   public static void moveFolder(String oldPath, String newPath) throws IOException
   { 
       copyFolder(oldPath, newPath); 
       delFolder(oldPath); 

   } 
}